<?php
if(defined("INC"))
{

/**
 * 
 * class Ftp
 * 
 * FTP functions
 * 
 */


class Ftp
{

	protected $connectionId = 0;


	/**
	 * Constructor
	 *
	 * @access public
	 *
	 */
	public function __construct($host, $login, $password, $local = false, $passiveMode = true)
	{

		$this->host = $host;
		$this->host_login = $login;
		$this->host_password = $password;
		$this->host_port = 21;
		$this->host_timeout = 30;
		$this->local = $local;
		$this->passive_mode = $passiveMode;

	}



	/**
	 * Connect to HOST FTP, set $this->connectionId on successful login
	 *
	 * @access public
	 *
	 * @return bool - Connection ok / failed
	 *
	 */
	public function connectFtp()
	{

		if(!$this->local)
		{
			$this->connectionId = ftp_connect($this->host, $this->host_port, $this->host_timeout);

			if(is_resource($this->connectionId))
			{
				$login_result = @ftp_login($this->connectionId, $this->host_login, $this->host_password);

				if((!$this->connectionId) || (!$login_result))
				{
					$this->connetion_id = 0;

					return false;
				}
				else
				{
					ftp_pasv($this->connectionId, $this->passive_mode);

					return true;
				}
			}
			else
			{
				return false;
			}

		}
		else
		{
			return true;
		}

	}



	/**
	 * Get file from HOST
	 *
	 * @param string $path_remote - Remote path to file
	 * @param string $path_local - Local path to file
	 * @param octal $chmod - Mode change (octal value)
	 *
	 * @access public
	 *
	 * @return bool - File get true/false
	 *
	 */
	public function getFile($path_remote, $path_local, $chmod = 0)
	{

		if(!$this->local)
		{
			$connection = (is_resource($this->connectionId)) ? true : $this->connectFtp();

			if($connection)
			{
				// Check if remote file exists
				if(ftp_size($this->connectionId, $path_remote) > 0)
				{
					if(ftp_get($this->connectionId, $path_local, $path_remote, FTP_BINARY))
					{
						if($chmod != 0)
						{
							chmod($path_local, $chmod);
						}

						return true;
					}
				}
			}
		}

		return false;

	}



	/**
	 * Put file to HOST
	 *
	 * @param string $path_local - Local path to file
	 * @param string $path_remote - Remote path to file
	 * @param octal $chmod - Mode change (octal value)
	 *
	 * @access public
	 *
	 * @return bool - File put true/false
	 *
	 */
	public function putFile($path_local, $path_remote, $chmod = 0)
	{

		if(!$this->local)
		{
			$connection = (is_resource($this->connectionId)) ? true : $this->connectFtp();

			if($connection)
			{
				if(ftp_put($this->connectionId, $path_remote, $path_local, FTP_BINARY))
				{
					if($chmod != 0)
					{
						$this->changeMode($path_remote, $chmod);
					}

					return true;
				}
			}
			else
			{
				return false;
			}
		}
		else
		{
			if(copy($path_local, $path_remote))
			{
				if($chmod != 0)
				{
					$this->changeMode($path_remote, $chmod);
				}

				return true;
			}
		}

	}



	/**
	 * Delete file
	 *
	 * @param string $path - Full path to file
	 *
	 * @access public
	 *
	 */
	public function deleteFile($path)
	{

		if(!$this->local)
		{
			if(ftp_delete($this->connectionId, $path))
			{
				return true;
			}
		}
		else
		{
			if(unlink($path))
			{
				return true;
			}
		}

		return false;

	}



	/**
	 * Make directory
	 *
	 * @param string $path - Full path to directory
	 * @param octal $chmod - Change mode (octal value)
	 *
	 * @access public
	 *
	 */
	public function makeDir($path, $chmod = 0)
	{

		if(!$this->local)
		{
			if(ftp_mkdir($this->connectionId, $path))
			{
				if($chmod != 0)
				{
					$this->changeMode($path, $chmod);
				}

				return true;
			}

			return false;
		}
		else
		{
			if(mkdir($path))
			{
				if($chmod != 0)
				{
					$this->changeMode($path, $chmod);
				}
			}

			return true;
		}

	}



	/**
	 * Change dir/file mode
	 *
	 * @param string $path - Path to dir/file
	 * @param octal $chmod - Mode change (octal value)
	 *
	 * @access public
	 *
	 */
	public function changeMode($path, $chmod)
	{

		$str_chmod = "0".$chmod;
		$dec_chmod = octdec($str_chmod);

		if(!$this->local)
		{
			if(ftp_chmod($this->connectionId, $dec_chmod, $path) !== false)
			{
				return true;
			}

			return false;
		}
		else
		{
			if(chmod($path, $dec_chmod) !== false)
			{
				return true;
			}

			return false;
		}

	}



	/**
	 * Close FTP connection
	 *
	 * @access public
	 *
	 */
	public function closeFtp()
	{

		if(!$this->local)
		{
			if(is_resource($this->connectionId))
			{
				ftp_close($this->connectionId); 
			}
		}

	}


}


}
else
{
	include_once "../init.php";
	include_once PATH_404_FILE;
}

?>